﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.NhomHangHoa
{
    public partial class frmChiTietNhomHang : Form
    {
        #region Cap nhap giao diện main
        public delegate void GetBoolean(Boolean capnhat);
        public GetBoolean GetData;
        #endregion

        #region Khoi Tao
        private Boolean kiemtra = false;
        private Entities.HangHoa banghi;
        private Entities.HangHoa[] hanghoa;
        private string ID;
        public frmChiTietNhomHang()
        {
            InitializeComponent();
        }
        private string maNhomHang;

        public string MaNhomHang
        {
            get { return maNhomHang; }
            set { maNhomHang = value; }
        }
        public frmChiTietNhomHang(string maNhomHang) 
        {
            InitializeComponent();
            this.maNhomHang = maNhomHang;
        }
        #region hiển thị
        private void Binding()
        {
            try
            {
                Entities.HangHoa gitri = new Entities.HangHoa(this.maNhomHang);
                hanghoa = Common.StaticVariable.InterfaceObject.SelectHangHoaTheoNhomHangHoa(gitri);
                dgvHienThi.DataSource = hanghoa;
                this.HienThiListview();
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                hanghoa = null;
                HienThiListview();
            }
        }
        private void HienThiListview()
        {
            try
            {
                if (hanghoa == null)
                {
                    Entities.HangHoa[] nv = new Entities.HangHoa[0];
                    dgvHienThi.DataSource = nv;
                }
                for (int i = 0; i < dgvHienThi.ColumnCount; i++)
                {
                    dgvHienThi.Columns[i].Visible = false;
                }
                dgvHienThi.Columns["HanhDong"].HeaderText = "STT";
                dgvHienThi.Columns["HanhDong"].Visible = true;
                dgvHienThi.Columns["MaHangHoa"].HeaderText = "Mã Hàng Hóa";
                dgvHienThi.Columns["MaHangHoa"].Visible = true;
                dgvHienThi.Columns["TenHangHoa"].HeaderText = "Tên Hàng Hóa";
                dgvHienThi.Columns["TenHangHoa"].Visible = true;
                dgvHienThi.Columns["GiaNhap"].HeaderText = "Giá Nhập";
                dgvHienThi.Columns["GiaNhap"].Visible = true;
                dgvHienThi.Columns["TenNhomHang"].HeaderText = "Tên Nhóm Hàng";
                dgvHienThi.Columns["TenNhomHang"].Visible = true;
                dgvHienThi.Columns["GiaBanBuon"].HeaderText = "Giá Bán Buôn";
                dgvHienThi.Columns["GiaBanBuon"].Visible = true;
                dgvHienThi.Columns["GiaBanLe"].HeaderText = "Giá Bán Lẻ";
                dgvHienThi.Columns["GiaBanLe"].Visible = true;
                if (dgvHienThi.RowCount > 0)
                {
                    Common.Utilities.CountDatagridview(dgvHienThi);
                    dgvHienThi.Focus();
                    dgvHienThi.Select();
                }
                dgvHienThi.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                dgvHienThi.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion
        #endregion
        private DateTime date;

        #region Them
        private void Them()
        {
            try
            {
                GUI.HangHoa.frmXuLy frm = new HangHoa.frmXuLy("Them", this.banghi);
                frm.ShowDialog();
            }
            catch (Exception ex)
            { string s = ex.Message; }
        }
        #endregion

        #region Sua
        private void Sua()
        {
            try
            {
                if (banghi != null)
                {
                    GUI.HangHoa.frmXuLy frm = new HangHoa.frmXuLy("Sua", this.banghi);
                    frm.ShowDialog();
                }
                else
                { MessageBox.Show("Chọn hàng hóa", Common.Constants.GiaTriCacForm.HeThongCanhBao); return; }
            }
            catch (Exception ex)
            { string s = ex.Message; }
        }
        #endregion

        #region Thoat
        private void Thoat()
        {
            try
            {
                System.Windows.Forms.DialogResult giatri = System.Windows.Forms.MessageBox.Show(Common.Constants.GiaTriCacForm.ThongBaoThoat, Common.Constants.GiaTriCacForm.HeThongCanhBao, System.Windows.Forms.MessageBoxButtons.YesNo);
                {
                    if (giatri == System.Windows.Forms.DialogResult.Yes)
                    {
                        kiemtra = true;
                        this.Close();
                    }
                }
            }
            catch (Exception ex)
            { string s = ex.Message; }
        }
        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            try
            {
                if (kiemtra != true)
                {
                    System.Windows.Forms.DialogResult giatri = System.Windows.Forms.MessageBox.Show(Common.Constants.GiaTriCacForm.ThongBaoThoat, Common.Constants.GiaTriCacForm.HeThongCanhBao, System.Windows.Forms.MessageBoxButtons.YesNo);
                    {
                        if (giatri == System.Windows.Forms.DialogResult.Yes)
                        {
                            this.Close();
                            kiemtra = false;
                        }
                        else
                        { e.Cancel = true; }
                    }
                }
            }
            catch (Exception ex)
            { string s = ex.Message; }
        }
       

        private void frmChiTietNhomHang_Load(object sender, EventArgs e)
        {
            this.Binding();
        }
        #endregion

        #region Processing
        //lay ban ghi de sua hoac xem
        private string MaHangHoa = string.Empty;
        private void GetRow()
        {
            try
            {
                if (hanghoa == null) { return; }
                for (int i = 0; i < this.hanghoa.Length; i++)
                {
                    if (hanghoa[i].MaHangHoa == this.MaHangHoa)
                    {
                        banghi = hanghoa[i];
                        break;
                    }
                    else
                    { continue; }
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion

        #region Tìm
        public void TimKiem()
        {
            dgvHienThi.DataSource = (Entities.HangHoa[])hanghoa.Where(p => p.MaHangHoa.ToUpper().IndexOf(txtTimKiem.Text.ToUpper()) >= 0).ToArray();
            HienThiListview();
        }
        #endregion

        #region sự kiện form
        private void dgvHienThi_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvHienThi.Rows.Count <= 0)
            { return; }
            //lay ma nhan vien tren gidview
            MaHangHoa = dgvHienThi.CurrentRow.Cells["MaHangHoa"].Value.ToString();
            this.GetRow();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Thoat();
        }

        private void btnthem_Click(object sender, EventArgs e)
        {
            Them();
        }

        private void btnsua_Click(object sender, EventArgs e)
        {
            Sua();
        }
        

        private void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            TimKiem();
        }

        private void btnTim_TextChanged(object sender, EventArgs e)
        {
            TimKiem();
        }

        private void dgvHienThi_DoubleClick(object sender, EventArgs e)
        {
            Sua();
        }
        #endregion
    }
}
